﻿//using Microsoft.AspNetCore.Builder;
//using Microsoft.AspNetCore.Hosting;
//using Microsoft.AspNetCore.Mvc;
//using Microsoft.Extensions.Configuration;
//using Microsoft.Extensions.DependencyInjection;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace Merp.Web
//{
//    public abstract class Startup
//    {
//        protected abstract string GetApiName();
//        protected abstract IAppBootstrapper GetAppBootstrapper(IConfigurationRoot configuration, IServiceCollection services);

//        public IConfigurationRoot Configuration { get; }

//        public Startup(IWebHostEnvironment env)
//        {
//            var builder = new ConfigurationBuilder()
//                .SetBasePath(env.ContentRootPath)
//                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
//                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

//            if (env.IsDevelopment())
//            {
//                // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
//                //builder.AddUserSecrets();
//            }

//            builder.AddEnvironmentVariables();
//            Configuration = builder.Build();
//        }

//        public void ConfigureServices(IServiceCollection services)
//        {
//            services.AddAuthorization();
//            services.AddAuthentication("Bearer")
//                .AddIdentityServerAuthentication(options =>
//                {
//                    options.Authority = Configuration["IdentityServerEndpoints:Authority"];
//                    options.RequireHttpsMetadata = true;

//                    options.ApiName = GetApiName();
//                });

//            RegisterClientsCors(services);

//            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
//            services.AddHttpContextAccessor();
//            services.AddSingleton(services);

//            var bootstrapper = GetAppBootstrapper(Configuration, services);
//            bootstrapper.Configure();
//        }


//        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
//        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
//        {
//            if (env.IsDevelopment())
//            {
//                app.UseDeveloperExceptionPage();
//            }
//            else
//            {
//                app.UseHsts();
//            }

//            app.UseHttpsRedirection();
//            UseClientsCors(app);
//            app.UseAuthentication();
//            app.UseMvc(routes =>
//            {
//                //routes.MapRoute(
//                //    name: "areas",
//                //    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
//                //  );

//                routes.MapRoute(
//                    name: "i18n",
//                    template: "api/private/{area:exists}/i18n/{controllerResourceName}/{actionResourceName}",
//                    defaults: new { controller = "Config", action = "GetLocalization" });

//                routes.MapRoute(
//                    name: "default",
//                    template: "api/private/{area:exists}/{controller}/{action}/{id?}");
//            });
//        }

//        private void RegisterClientsCors(IServiceCollection services)
//        {
//            services.AddCors(options =>
//            {
//                options.AddPolicy("WebApp", policy =>
//                {
//                    policy.WithOrigins(Configuration["ClientEndpoints:WebApp"])
//                        .AllowAnyHeader()
//                        .AllowAnyMethod();
//                });
//            });
//        }

//        private void UseClientsCors(IApplicationBuilder app)
//        {
//            app.UseCors("WebApp");
//        }
//    }
//}
